# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=luarocks
pkgver=2.4.4
pkgrel=1
pkgdesc="Deployment and management system for Lua modules"
url="http://www.luarocks.org/"
arch="noarch"
license="MIT"
depends="curl"
makedepends=""
subpackages=""
triggers=""
source="http://luarocks.org/releases/$pkgname-$pkgver.tar.gz
	fix-tree-rocks_dir.patch
	config.lua"
builddir="$srcdir/$pkgname-$pkgver"

_lua_versions="5.1 5.2 5.3"
for _v in $_lua_versions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages $pkgname${_v}:_subpackage"
	triggers="$triggers $pkgname$_v.trigger=/usr/lib/luarocks/rocks-$_v:/usr/lib/luarocks/rocks-common"
done

prepare() {
	default_prepare

	# Create builddir for each Lua version.
	local lver; for lver in $_lua_versions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_lua_versions; do
		msg "Building for Lua $lver..."
		cd "$builddir-$lver"

		./configure \
			--prefix=/usr \
			--sysconfdir=/etc/luarocks \
			--rocks-tree=/usr/local \
			--lua-version="$lver" \
			--lua-suffix="$lver" \
			--with-lua-include="$(pkg-config --variable=includedir lua$lver)" \
			--with-lua-lib="$(pkg-config --variable=libdir lua$lver)" \
			--with-downloader=curl \
			--with-md5-checker=md5sum \
			--versioned-rocks-dir
		make
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname#$pkgname}"
	pkgdesc="Deployment and management system for Lua $lver modules"
	install_if="lua$lver $pkgname=$pkgver-r$pkgrel"
	depends="$depends lua$lver"

	cd "$builddir-$lver"

	make install DESTDIR="$subpkgdir"

	# Remove default script names which conflict per Lua version.
	rm "$subpkgdir"/usr/bin/luarocks \
		"$subpkgdir"/usr/bin/luarocks-admin

	# Set the correct Lua version.
	sed -i "s/env lua/env lua${lver}/" \
		"$subpkgdir"/usr/bin/luarocks-$lver \
		"$subpkgdir"/usr/bin/luarocks-admin-$lver

	# Config based on:
	#   https://github.com/luarocks/luarocks/wiki/Integrating-distro-modules-with-LuaRocks
	install -D -m 644 "$srcdir"/config.lua "$subpkgdir"/etc/$pkgname/config-$lver.lua
}

sha512sums="cbc6a54aa5077e656dcb4fed915fe8b66cad4ad04f28700e2f67805232a6dc366d4ac03e3a85e27a45b178c362ae8aa905ceab5553252735e049730a28450e19  luarocks-2.4.4.tar.gz
22ca5efa62c592c8395ca8c9ce3c938b5e41cf7d149f45d3666a5370d27e3e8c01d1712217c456d7b54163e375d9841d5f9213b25f4237b0bf3ab498c02864fb  fix-tree-rocks_dir.patch
1f75705af3b1ef57386234195a5d496d554c95473e5b0c0a3319e3c0f905519397b92603c5596d363818df7d94f886b898ba12a3880d3e982d1c63b74c50febe  config.lua"
